<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input :placeholder="`${$t('common.table.id')}/${$t('member.cellphone')}/${$t('member.name')}`" v-model="listQuery.title" style="width: 200px;" class="filter-item"
        clearable @keyup.enter.native="handleFilter" />
      <span class="filter-item">
        <el-date-picker v-model="listQuery.timeInterval" :picker-options="pickerOptions" :range-separator="$t('common.to')" :start-placeholder="$t('date_picker.start_placeholder')"
          :end-placeholder="$t('date_picker.end_placeholder')" type="datetimerange" align="right" value-format="yyyy-MM-dd HH:mm:ss" />
      </span> -->
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('common.search') }}</el-button> -->
      <!-- <el-button v-permission="$store.jurisdiction.MemberExport" :loading="formLoading" class="filter-item" style="margin-left: 10px;float:right;" icon="el-icon-download"
        @click="handleExport">{{ $t('common.export') }}</el-button>
      <el-button v-permission="$store.jurisdiction.MemberCreate" class="filter-item" style="margin-left: 10px;float:right;" type="primary" icon="el-icon-edit"
        @click="handleCreate">{{ $t('common.add') }}</el-button> -->
      <!--<el-button v-permisssion="jurisdiction.usersListExport" v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">{{ $t('usuel.export') }}</el-button>-->
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item :label="$t('good.search.form.title.label')">
          <el-input v-model="listQuery.keyword" placeholder="账号" clearable @keyup.enter.native="handleFilter" />
        </el-form-item>
        <span class="filter-item">
          <el-date-picker v-model="listQuery.timeInterval" :picker-options="pickerOptions" :range-separator="$t('common.to')"
            :start-placeholder="$t('date_picker.start_placeholder')" :end-placeholder="$t('date_picker.end_placeholder')" type="datetimerange" align="right"
            value-format="yyyy-MM-dd" format="yyyy-MM-dd" />
        </span>
        <el-form-item>
          <el-button type="primary" @click="handleFilter">{{ $t('common.search') }}</el-button>
          <el-button :loading="formLoading" class="filter-item" style="margin-left: 10px;float:right;" icon="el-icon-download"
            @click="handleExport">{{ $t('common.export') }}</el-button>
          <!-- {{ $t('good.search.form.category_id.label') }}: -->
          <!-- <el-cascader v-model="listQuery.category_id" :options="categorys" :props="{ expandTrigger: 'hover' }" clearable @change="changeCategorys" /> -->
        </el-form-item>
      </el-form>

    </div>
    <div class="operation" style="margin-bottom: 10px;">
      <el-button size="mini" @click="handleCheckAllChange">{{ $t('common.check_all') }}/{{ $t('common.inverse') }}</el-button>

    </div>
    <template>
      <el-table v-loading="listLoading" ref="multipleTable" :data="list" border fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
        <el-table-column type="selection" width="55" fixed="left" />
        <el-table-column label="账号ID">
          <template slot-scope=" scope">
            {{ scope.row.user.cellphone }}
          </template>
        </el-table-column>
        <el-table-column label="用户名" width="200">
          <template slot-scope="scope">
            {{ scope.row.user.nickname }}
          </template>
        </el-table-column>
        <el-table-column label="积分收入" width="200">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.integral > 0">{{ scope.row.integral||0 }}</el-tag>
            <el-tag v-else type="danger">{{ scope.row.integral||0 }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="上次结余">
          <template slot-scope="scope">
            {{ scope.row.user_integral }}
          </template>
        </el-table-column>
        <el-table-column label="剩余积分">
          <template slot-scope="scope">
            {{ scope.row.user.integral }}
          </template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <!-- <el-table-column :label="$t('good.table.updated_at')" sortable="custom" prop="updated_at">
          <template slot-scope="scope">
            <span>{{ scope.row.updated_at }}</span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作时间" sortable="custom" prop="created_at">
          <template slot-scope="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>

      </el-table>
      <!--分页-->
      <div class="pagination-operation">
        <pagination v-show="total>0" :page-sizes="[10,50,100, 200, 500, 1000]" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" class="pagination"
          @pagination="getList" />
      </div>
    </template>

  </div>
</template>

<script>
import { getPonitsList, exports } from '@/api/points'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination'

export default {
  name: 'PointsList',
  components: { Pagination },
  directives: { waves },
  data() {
    var validateMobile = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('hint.error.please_enter', { attribute: this.$t('member.cellphone') })))
      } else {
        if (!(/^\d+$/.test(value))) {
          callback(new Error(this.$t('hint.error.wrong_format', { attribute: this.$t('member.cellphone') })))
        }
        callback()
      }
    }
    return {
      formLoading: false,
      actionurl: process.env.BASE_API + 'uploadPictures',
      uploadData: {},
      pickerOptions: {
        shortcuts: [{
          text: this.$t('usuel.latest_week'),
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: this.$t('usuel.last_month'),
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: this.$t('usuel.last_three_months'),
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      imageUrl: '',
      portrait: '',
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      textMap: {
        update: this.$t('common.amend'),
        create: this.$t('common.add')
      },
      listLoading: true,
      state: 0,
      listQuery: {
        activeIndex: 0,
        type: 0,
        page: 1,
        limit: 10,
        sort: '-id'
      },
      temp: {
        password: '',
        gender: 0,
        name: '',
        cellphone: '',
        nickname: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      adminRules: {
        name: [
          { required: true, message: this.$t('hint.error.please_enter', { attribute: this.$t('member.name') }), trigger: 'blur' }
        ],
        cellphone: [
          { required: true, message: this.$t('hint.error.please_enter', { attribute: this.$t('member.cellphone') }), trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('hint.error.please_enter', { attribute: this.$t('member.password') }), trigger: 'blur' }
        ]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSelectionChange(val) { // 设置全选/全不选数据
      console.log('🚀 ~  val: ', val)
      this.multipleSelection = val
    },
    handleCheckAllChange() {
      this.$refs.multipleTable.toggleAllSelection()
    },

    getList() {
      this.listLoading = true
      console.log('🚀 ~  listLoading: ', 11)
      getPonitsList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        state: 1,
        password: '',
        gender: 0,
        name: '',
        cellphone: '',
        nickname: ''
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (order === 'ascending') {
        this.listQuery.sort = '+' + prop
      } else {
        this.listQuery.sort = '-' + prop
      }
      this.handleFilter()
    },
    handleExport() {
      this.formLoading = true
      exports(this.listQuery).then(response => {
        window.open(response.data)
      }).finally(res => {
        this.formLoading = false
      })
    }

  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.timeInterval {
  top: -4px;
}
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 90px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
